/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.unibo.as.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import java.util.ArrayList;
import java.util.Iterator;
import it.unibo.as.client.Common.SeedCard;

/**
 *
 * @author Johnny
 */
public class TablePanel extends AbsolutePanel {

    final static int CARD_HEIGHT = 143;
    final static int CARD_WIDTH = 100;

    final static int TABLE_HEIGHT = 524;
    final static int TABLE_WIDTH = 1024;

    final static String CARD_BACK = "back-blue-150-1";

    final static int MAX_HAND_Y = TABLE_HEIGHT - (CARD_HEIGHT/2);

    final static String DECKPATH = "resources/images/deck/";

    private static ArrayList<Image> handArray;

    private Boolean myTurn;

    public TablePanel() {
        myTurn = false;

        setHeight( TABLE_HEIGHT + "px");
        Image image = new Image("resources/images/greentable.png");
        image.setSize(TABLE_WIDTH + "px", TABLE_HEIGHT + "px");
        image.setStyleName("tableback");
        add(image, 0, 0);

        handArray = new ArrayList<Image>();


        /* TEST DRAW CARDS */

        Card testCard1 = new Card(SeedCard.DIAMONDS ,5);
        Card testCard2 = new Card(SeedCard.HEARTS ,9);
        Card testCard3 = new Card(SeedCard.SPADES ,7);
        Card testCard4 = new Card(SeedCard.DIAMONDS ,1);

        drawPlayed(0,testCard1);
        drawPlayed(1,testCard2);
        drawPlayed(2,testCard3);
        drawPlayed(3,testCard4);


        /* END TEST */

        /* TEST DRAW HAND */

        ArrayList<Card> hand = new ArrayList<Card>();
        hand.add(testCard4);
        hand.add(testCard1);
        hand.add(testCard2);
        hand.add(testCard3);
        hand.add(testCard4);
        hand.add(testCard1);
        hand.add(testCard4);
        hand.add(testCard3);
        hand.add(testCard2);
        hand.add(testCard1);
        hand.add(testCard2);

        drawHand(hand);

        /* END TEST */

        /* TEST DRAW BLIND HAND */

        drawBlindHand(1,12);
        drawBlindHand(2,12);
        drawBlindHand(3,12);

        /* END TEST */



    }

    public void drawPlayed(int position, Card card){
        String cardName;

        cardName = card.toString();


        Image image = new Image(DECKPATH + cardName + ".png");
        image.setSize( CARD_WIDTH + "px", CARD_HEIGHT + "px");
        image.setAltText(cardName);
        image.setStyleName("card" + position);
        image.addStyleName("hand" + position);


        switch(position){
            case 0:
                add(image, (TABLE_WIDTH /2) - (CARD_WIDTH/2), (TABLE_HEIGHT/2));
                break;
            case 1:
                add(image, ((TABLE_WIDTH /2) - CARD_HEIGHT) , ((TABLE_HEIGHT/2) - CARD_WIDTH/2) );
                break;
            case 2:
                add(image, (TABLE_WIDTH /2) - (CARD_WIDTH/2), ((TABLE_HEIGHT/2) - (CARD_HEIGHT/4*3)) );
                break;
            case 3:
                add(image, (TABLE_WIDTH /2) + (CARD_WIDTH/2), ((TABLE_HEIGHT/2) - CARD_WIDTH/2));
                break;
            default:
                break;

        }

    }

    public void drawHand(ArrayList<Card> hand){

        int index=0;
        int ypos= MAX_HAND_Y;

        int size = hand.size();

        Iterator handIterator = hand.iterator();

        Iterator handArrayIterator = handArray.iterator();
        Image image;

        /* Se è stata disegnata prima la mano elimina le immagini */
        while(handArrayIterator.hasNext()){
            ((Image)handArrayIterator.next()).removeFromParent();
        }
        

        while(handIterator.hasNext()){
           index++;
           final Card currentCard = (Card)handIterator.next();


           image = new Image(DECKPATH + currentCard.toString() + ".png");
           image.setSize( CARD_WIDTH + "px", CARD_HEIGHT + "px");
           image.setAltText(currentCard.toString());
           image.setStyleName("hand" + index);

           final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean result) {
                    if(result){

                    }
                    else{
                        com.google.gwt.user.client.Window.alert("DEBUG: CARD NOT PLAYED. TOO BAD.");
                    }

                }

                @Override
                public void onFailure(Throwable caught) {
                   com.google.gwt.user.client.Window.alert("DEBUG: CARD NOT PLAYED. ERROR!.");

                }
         };



           image.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    if(myTurn){
                        getGameService().playCard(currentCard, callback);
                        ((Image)event.getSource()).removeFromParent();
                        myTurn = false;
                    }
                    
                }
           });

           if(index <= (size/2)){
               ypos = MAX_HAND_Y - (index *2);
           }
           else{
               ypos = MAX_HAND_Y - ((size%index) *2);
           }


           add(image, (TABLE_WIDTH/2) - ((CARD_WIDTH/2)*(size/2 + 2)) + (CARD_WIDTH/2 * index), ypos );

           /*aggiungo all'array per poi resettarlo al cambiamento dello stato */
           handArray.add(image);

        }
    }

    public void drawBlindHand(int position, int n){
        int c;
        int xpos = 0;
        int ypos;
        Image image;

        for(c=0;c<n;c++){
           image = new Image(DECKPATH + CARD_BACK + ".png");
           image.setSize( CARD_WIDTH + "px", CARD_HEIGHT + "px");
           image.setAltText("Hidden Card");
           image.setStyleName("hand" + c);
           image.addStyleName("card" + position);
           if(position%2==0){ //posizioni 2 e 0
                if(c <= (n/2)){

                    ypos = (-50) + (c *2);
                }
                else{
                    ypos = (-50) + ((n%c) *2);
                }

                add(image, (TABLE_WIDTH/2) - ((CARD_WIDTH/2)*(n/2)) + (CARD_WIDTH/2 * c), ypos );
           }
           if(position%2==1){ //posizioni 1 e 3
               //x diverse per quadrante 1 e quadrante 3

                if(c <= (n/2)){
                    if(position==1){
                        xpos = 20 + (c *2);
                    }
                    else if(position==3){
                        xpos = (TABLE_WIDTH - 100) - (c *2);
                    }
                }
                else{
                    if(position==1){
                        xpos = 20 + ((n%c) *2);
                    }
                    else if(position==3){
                        xpos = (TABLE_WIDTH - 100) - ((n%c) *2);
                    }

                }

                // stesse y per quadrante 1 e quadrante 3
                add(image,xpos, (TABLE_HEIGHT/2) - ((CARD_WIDTH/4)*(n/2 + 2)) + (CARD_WIDTH/4 * c));
           }
           


        }
    }

    public static GWTGameServiceAsync getGameService() {
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of the interface. The cast is always safe because the
        // generated proxy implements the asynchronous interface automatically.

        return GWT.create(GWTGameService.class);
    }

}
